package org.juggers.services;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.HashSet;
import java.util.Set;

import javax.persistence.NoResultException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.juggers.component.MailSender;
import org.juggers.domain.AlreadyConfirmedException;
import org.juggers.domain.AlreadyRegisteredException;
import org.juggers.domain.InvalidTokenException;
import org.juggers.domain.Jugger;
import org.juggers.domain.SendMailException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
@Service
public class DefaultRegistrationService
    implements RegistrationService
{
    private static final Log logger = LogFactory.getLog( DefaultRegistrationService.class );
    
    @Autowired
    private MailSender mailSender;

    /**
     * {@inheritDoc}
     * 
     * @see org.juggers.service.RegistrationService#register(org.juggers.domain.Jugger)
     */
    @Transactional
    public void register( Jugger jugger )
        throws AlreadyRegisteredException, SendMailException
    {
        try {
        String eMail = jugger.getEMail();
        try
        {
            Jugger.findByEMail( eMail );
            throw new AlreadyRegisteredException();
        }
        catch ( NoResultException ex )
        {

        }

        Set<String> roles = new HashSet<String>();
        roles.add( "ROLE_JUGGER" );
        jugger.setRoles( roles );

        jugger.setPending();
        jugger.persist();

        // send mail
        mailSender.send( "subscription", "Juggers.org: Votre inscription", jugger );
        }
        catch (Exception ex) {
            logger.error( "Erreur : ", ex );
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.juggers.service.RegistrationService#confirm(java.lang.String, java.lang.String)
     */
    @Transactional
    public Jugger confirm( String eMail, String token )
        throws AlreadyConfirmedException, InvalidTokenException
    {
        Jugger jugger = null;
        try
        {
            jugger = Jugger.findByEMail( eMail );
        }
        catch ( NoResultException ex )
        {
            throw new InvalidTokenException();
        }

        jugger.confirm( token );
        return jugger;
    }
}
